package com.wei.czz.common.enums.common.dict;

import com.wei.czz.common.dto.common.NameValue;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2022-08-14 09:57:51
 * className: DictEnum 字典类型枚举类
 * version: 1.0
 * description:
 */
@Getter
@AllArgsConstructor
public enum DictEnum {
    /*
        系统字典公共字典值
     */
    SYSTEM_USER(1, "系统超级管理员用户字典", Boolean.TRUE),

    DEFAULT_MENU_ROLE(2, "新增菜单默认关联角色字典", Boolean.TRUE),

    DEFAULT_USER_DEPT(3, "新用户注册自动关联部门字典", Boolean.FALSE),

    DEFAULT_USER_ROLE(4, "新用户注册默认关联角色字典", Boolean.FALSE),

    DEFAULT_USER_AVATAR(5, "用户默认头像字典", Boolean.FALSE),

    DEFAULT_AVATAR(6, "网站用户默认可选头像字典", Boolean.TRUE),

    MAX_LOCK_NUM(9, "一天允许最大被锁定次数字典", Boolean.FALSE),

    MAX_COUNT_OPT(10, "一分钟允许最大请求次数字典", Boolean.FALSE),

    MAX_LAST_COUNT(11, "一分钟允许最大连续请求次数字典", Boolean.FALSE),

    STORE_AUDIT_ROLE(1000, "商店审核角色字典", Boolean.FALSE),

    IP_MAX_REQUEST_FAIL_NUM(2000, "单ip一分钟允许最大请求失败次数", Boolean.FALSE),

    USER_MAX_REQUEST_FAIL_NUM(2001, "单用户一分钟允许最大请求失败次数", Boolean.FALSE),

    IP_MAX_SMS(3000, "单ip一天允许发送短信次数", Boolean.FALSE),

    BLOG_AUDIT_ROLE(4000, "博客审核角色", Boolean.TRUE),

    BLOG_AUDIT_KPI(4100, "博客审核KPI", Boolean.FALSE),

    CHUNK_FILE_SIZE(5000, "切片文件大小", Boolean.FALSE),

    ;

    /**
     * 值
     */
    private final Integer value;

    /**
     * 名称
     */
    private final String name;

    /**
     * 字典是否允许添加多次[true-允许 false-不允许]
     */
    private final Boolean multiFlag;

    public Long getLongValue() {
        return (long) value;
    }

    public String getStringValue() { return value.toString(); }

    public static String findDictName(Integer type) {
        DictEnum dictEnum = getDict(type);
        if (dictEnum != null) {
            return dictEnum.name;
        }
        return null;
        /*DictEnum[] dictEnums = getDictTypes();
        int lo = 0, hi = dictEnums.length - 1;
        while (lo <= hi) {
            int mid = lo + (hi - lo) / 2;
            int res = dictEnums[mid].value.compareTo(type);
            if (res > 0) {
                hi = mid - 1;
            } else if (res < 0) {
                lo = mid + 1;
            } else {
                return dictEnums[mid].getDesc();
            }
        }
        return null;*/
    }

    /**
     * 获取字典类型列表
     * @return 字典类型列表
     */
    public static List<NameValue<Integer>> getDictTypeList() {
        return Arrays.stream(values())
                .filter(dictEnum -> Objects.nonNull(dictEnum.multiFlag))
                .map(dictEnum -> new NameValue<>(dictEnum.name, dictEnum.value))
                .collect(Collectors.toList());
    }

    /**
     * 二分查找字典枚举对象
     * @param value 值对象
     * @return 字典枚举对象
     */
    public static DictEnum getDict(Integer value) {
        List<DictEnum> dictEnumList = Arrays.stream(values())
                .filter(dictEnum -> Objects.nonNull(dictEnum.multiFlag))
                .collect(Collectors.toList());
        int lo = 0, hi = dictEnumList.size() - 1;
        while (lo <= hi) {
            int mid = (lo + hi) >> 1;
            int res = dictEnumList.get(mid).value.compareTo(value);
            if (res > 0) {
                hi = mid - 1;
            } else if (res < 0) {
                lo = mid + 1;
            } else {
                return dictEnumList.get(mid);
            }
        }
        return null;
    }

}

